package mesweb.mesweb.controller.procedure;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.Procedure.MesProcedureTechnology;
import mesweb.mesweb.service.procedure.IMesProcedureTechnologyService;
import mesweb.mesweb.vo.procecdure.MesProcedureTechnologyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 工序工艺关系
 * @Author: jeecg-boot
 * @Date:   2021-06-25
 * @Version: V1.0
 */

@Api(tags="工序工艺关系")
@RestController
@RequestMapping("/mes/mesProcedureTechnology")
public class MesProcedureTechnologyController {
	@Autowired
	private IMesProcedureTechnologyService mesProcedureTechnologyService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam( value = "procedureId",required = false) Long procedureId) {
        List<MesProcedureTechnologyVo> data = new ArrayList<>();
		QueryWrapper<MesProcedureTechnologyVo> queryWrapper = new QueryWrapper<>();
        if (procedureId >0L) {
            queryWrapper.eq("procedure_id",procedureId);
        } else {
        }
        data = mesProcedureTechnologyService.getProcedureTechnology(queryWrapper);
        return CommonResult.success(data);
    }
	/**
	 * 分页列表查询
	 *
	 * @param mesProcedureTechnology
	 * @param page
	 * @param page
	 * @param req
	 * @return
	 */
	@ApiOperation(value="工序工艺关系-分页列表查询", notes="工序工艺关系-分页列表查询")
	@GetMapping(value = "/list")
	public CommonResult<?> queryPageList(MesProcedureTechnologyVo mesProcedureTechnology,
								   @RequestParam(name="page", defaultValue="1") Integer page,
								   @RequestParam(name="size", defaultValue="10") Integer size,
								   HttpServletRequest req) {
		QueryWrapper<MesProcedureTechnologyVo> queryWrapper = new QueryWrapper<>();
		Page<MesProcedureTechnologyVo> pageMesProcedureTechnology = new Page<>(page, size);
		IPage<MesProcedureTechnologyVo> pageList = mesProcedureTechnologyService.getProcedureTechnology(pageMesProcedureTechnology, queryWrapper);
		return CommonResult.success(pageList);
	}
	/**
	 * 编辑
	 *
	 * @param mesProcedureTechnology
	 * @return
	 */
	@ApiOperation(value="工序工艺关系-编辑", notes="工序工艺关系-编辑")
	@PostMapping(value = "/save")
	public CommonResult<?> edit(@RequestBody MesProcedureTechnology mesProcedureTechnology) {
       boolean result = false;
        if(mesProcedureTechnology.getId() == 0L) {
             result = mesProcedureTechnologyService.save(mesProcedureTechnology);
        }
        else
        {
            result = mesProcedureTechnologyService.updateById(mesProcedureTechnology);
        }
        return CommonResult.success(result);
	}

	/**
	 * 编辑
	 *
	 * @param mesTechnologySetFlows
	 * @return
	 */
	@ApiOperation(value="工艺组工作流程关系-批量增加", notes="工艺组工作流程关系-批量增加")
	@PostMapping(value = "/saveBatch")
	public CommonResult<?> edit(@RequestBody Collection<MesProcedureTechnology> mesTechnologySetFlows) {
		boolean result = false;
		try {
			if (mesTechnologySetFlows.size() > 0L) {
				for (MesProcedureTechnology mesTechnologySetFlow :
						mesTechnologySetFlows) {
					result = mesProcedureTechnologyService.save(mesTechnologySetFlow);
				}
			}
			return CommonResult.success(mesTechnologySetFlows);
		}catch (Exception e)
		{
			return  CommonResult.failed(e.getMessage());
		}
	}
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="工序工艺关系-通过id删除", notes="工序工艺关系-通过id删除")
	@DeleteMapping(value = "/delete")
	public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
		mesProcedureTechnologyService.removeById(id);
		return CommonResult.success("删除成功!");
	}
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="工序工艺关系-批量删除", notes="工序工艺关系-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public CommonResult<?> deleteBatch(@ApiParam("MesProcedureTechnologyIDs") @RequestBody List<Long> ids) {
		this.mesProcedureTechnologyService.removeByIds(ids);
		return CommonResult.success("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="工序工艺关系-通过id查询", notes="工序工艺关系-通过id查询")
	@GetMapping(value = "/queryById")
	public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
		MesProcedureTechnology mesProcedureTechnology = mesProcedureTechnologyService.getById(id);
		return CommonResult.success(mesProcedureTechnology);
	}
}
